using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace _1942
{
    /// <summary>
    /// This class is the Animated sprite for the Boss
    /// </summary>
    public class Boss : Sprite
    {
        #region Variables

        // Vertical velocity
        protected float Yspeed;

        // Horizontal velocity
        protected int Xspeed;
        
        protected Random random;

        //Stops the plane from firing nonstop
        private bool canShoot = false;

        //Time between shots
        private const int SHOOTTIME = 30;
        private int shootTimer = 0;
        

        private static List<Rectangle> planeAni = new List<Rectangle>();
        private static List<Rectangle> explosionAni = new List<Rectangle>();

        //Window size
        private static int windowHeight = -1;
        private static int windowWidth = -1;

        private static Rectangle frame = new Rectangle();

        //If between  kill and vacant
        private bool exploding = false;

        //how much hits the plane can take before dying
        private const int HP = 100;

        //current health
        private int health = HP;

        private Color[] texturesData;

        private bool isDefeated = false;
        public bool IsDefeated
        {
            get { return isDefeated; }
        }

        #endregion

        #region Constructors

        public Boss(Game game, ref Texture2D theTexture) :  base(game, ref theTexture)
        {
            //Sets the window size
            if (windowHeight == -1)
            {
                windowHeight = Game.Window.ClientBounds.Height;
                windowWidth = Game.Window.ClientBounds.Width;
            }

            //Sets all the frames for the animations
            #region frames
            if (planeAni.Count == 0)
            {
                frame.X = 394;
                frame.Y = 591;
                frame.Width = 212;
                frame.Height = 180;
                planeAni.Add(frame);
            }
            Frames = planeAni;
            currentFrame = Frames[0];
            #endregion

            //Sets all the frames for the animations
            #region Explosionframes
            if (explosionAni.Count == 0)
            {
                frame.X = 70;
                frame.Y = 170;
                frame.Width = 32;
                frame.Height = 32;
                explosionAni.Add(frame);

                frame.X = 103;
                explosionAni.Add(frame);

                frame.X = 136;
                explosionAni.Add(frame);

                frame.X = 169;
                explosionAni.Add(frame);

                frame.X = 201;
                explosionAni.Add(frame);

                frame.X = 234;
                explosionAni.Add(frame);
                frame.X = 234;
                explosionAni.Add(frame);
                frame.X = 234;
                explosionAni.Add(frame);
            }
            #endregion


            // Initialize the random number generator and put the plane in it's starting position
            random = new Random(GetHashCode());

            //radomizes when to start shooting
            shootTimer = random.Next(200);
            PutinStartPosition();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Initialize plane Position and Velocity
        /// </summary>
        public void PutinStartPosition()
        {
            Position = new Vector2(windowWidth - currentFrame.Width, 0);
            YSpeed = 1;
            XSpeed = 0;
        }

        /// <summary>
        /// Update the plane Position
        /// </summary>
        public override void Update(GameTime gameTime)
        {
           if (Position.Y > 150 || (Position.Y < 10 && YSpeed<0)) Yspeed = -Yspeed;
           if (Position.X > windowWidth || Position.Y < 10) Xspeed = -Xspeed;

            // Move plane
            Position = new Vector2(Position.X + Xspeed, Position.Y + Yspeed);

            //Count down shootTimer
            shootTimer--;
            if (shootTimer < 0) canShoot = true;

            if (exploding)
            {
                if (activeFrame == explosionAni.Count - 1)
                {
                    //PutinStartPosition();
                    isDefeated = true;
                    MakeObsolete();
                }
            }

            base.Update(gameTime);
            
        }

        private void MakeObsolete()
        {
            Position = new Vector2(-50, -50);
            Visible = false;
            Enabled = false;
        }

        internal void kill()
        {
            explode();
        }

        private void explode()
        {
            exploding = true;
            Yspeed = 0.75f;
            frameDelay = (long)0.9;
            //Sets all the frames for the animations
            #region frames
            Rectangle frame = new Rectangle();
            Frames.Clear();
            frame.X = 70;
            frame.Y = 170;
            frame.Width = 32;
            frame.Height = 32;
            Frames.Add(frame);

            frame.X = 103;
            Frames.Add(frame);

            frame.X = 136;
            Frames.Add(frame);

            frame.X = 169;
            Frames.Add(frame);

            frame.X = 201;
            Frames.Add(frame);

            frame.X = 234;
            Frames.Add(frame);
            frame.X = 234;
            Frames.Add(frame);
            frame.X = 234;
            Frames.Add(frame);
            activeFrame = 0;
            #endregion
        }

        public void hit()
        {
            health--;
            if(health < 1) kill();

        }

        #endregion

        #region Properties

        /// Vertical velocity
        /// </summary>
        public float YSpeed
        {
            get { return Yspeed; }
            set
            {
                Yspeed = value;
                frameDelay = (long)(200 - (Yspeed * 5));
            }
        }

        /// <summary>
        /// Horizontal Velocity
        /// </summary>
        public int XSpeed
        {
            get { return Xspeed; }
            set { Xspeed = value; }
        }

        public Color[] TextureData
        {
            get { return texturesData; }
        }

        public bool CanShoot
        {
            get { return canShoot; }
            set
            {
                canShoot = value;
                shootTimer = SHOOTTIME;
            }
        }

        public bool Exploding
        {
            get { return exploding; }
        }

        #endregion

    }
}